package com.yqya.springboot.pattern.strategy.biz.handler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 订单处理器上下文
 *
 * @author Kevin
 * @date 2019-06-29
 */
public class OrderHandlerContext {

    private Map<String, Class> orderHandlerMap = new ConcurrentHashMap<>();

    public OrderHandlerContext(Map<String, Class> orderHandlerMap) {
        this.orderHandlerMap.putAll(orderHandlerMap);
    }

    public AbstractOrderHandler getInstance(String type) {
        Class clazz = orderHandlerMap.get(type);
        if (clazz == null) {
            throw new IllegalArgumentException("not found handler for type: " + type);
        }

        // 获取bean
        return (AbstractOrderHandler) BeanTool.getBean(clazz);
    }
}
